package cz.data.domain.quality.controller;

import cz.data.common.base.BaseResponse;
import cz.data.common.base.SimpleDataResponse;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cz.data.common.base.BaseController;
import cz.data.common.core.JsonPage;
import cz.data.domain.quality.mapstruct.ScheduleLogMapper;
import cz.data.domain.quality.model.entity.ScheduleLogEntity;
import cz.data.domain.quality.model.query.ScheduleLogQuery;
import cz.data.domain.quality.model.vo.ScheduleLogVo;
import cz.data.domain.quality.service.ScheduleLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Api(tags = {"数据质量监控任务日志"})
@RestController
@RequestMapping("/scheduleLogs")
public class ScheduleLogController extends BaseController {

    @Autowired
    private ScheduleLogService scheduleLogService;

    @Autowired
    private ScheduleLogMapper scheduleLogMapper;

    @ApiOperation(value = "数据质量监控任务日志-详情")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path")
    @GetMapping("/{id}")
    public SimpleDataResponse<ScheduleLogVo> getScheduleLogById(@PathVariable String id) {
        ScheduleLogEntity scheduleLogEntity = scheduleLogService.getScheduleLogById(id);
        return new SimpleDataResponse<>(scheduleLogMapper.toVO(scheduleLogEntity));
    }

    @ApiOperation(value = "数据质量监控任务日志-分页查询")
    @ApiImplicitParam(name = "scheduleLogQuery", value = "查询实体scheduleLogQuery", required = true, dataTypeClass = ScheduleLogQuery.class)
    @GetMapping("/page")
    public SimpleDataResponse<JsonPage<ScheduleLogVo>> getScheduleLogPage(ScheduleLogQuery scheduleLogQuery) {
        QueryWrapper<ScheduleLogEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(scheduleLogQuery.getExecuteJobId()), "l.execute_job_id", scheduleLogQuery.getExecuteJobId());
        queryWrapper.eq(StringUtils.isNotBlank(scheduleLogQuery.getRuleTypeId()), "t.id", scheduleLogQuery.getRuleTypeId());
        IPage<ScheduleLogEntity> page = scheduleLogService.page(new Page<>(scheduleLogQuery.getPageNum(), scheduleLogQuery.getPageSize()), queryWrapper);
        List<ScheduleLogVo> collect = page.getRecords().stream().map(scheduleLogMapper::toVO).collect(Collectors.toList());
        JsonPage<ScheduleLogVo> jsonPage = new JsonPage<>(page.getCurrent(), page.getSize(), page.getTotal(), collect);
        return new SimpleDataResponse<>(jsonPage);
    }

    @ApiOperation(value = "数据质量监控任务日志-删除")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path")
    @DeleteMapping("/{id}")
    public BaseResponse deleteScheduleLogById(@PathVariable String id) {
        scheduleLogService.deleteScheduleLogById(id);
        return BaseResponse.success();
    }

    @ApiOperation(value = "数据质量监控任务日志-批量删除")
    @ApiImplicitParam(name = "ids", value = "ID集合", required = true, dataType = "List", paramType = "path")
    @DeleteMapping("/batch/{ids}")
    public BaseResponse deleteScheduleLogBatch(@PathVariable List<String> ids) {
        scheduleLogService.deleteScheduleLogBatch(ids);
        return BaseResponse.success();
    }
}
